package net.test.piggy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.test.piggy.mapper.TestMapper;
import net.test.piggy.model.po.UserPO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class MybatisController {
    @Resource
    private TestMapper testMapper;
    @GetMapping("mybatis01")
    @ResponseBody
    public IPage<?> select(){
        Page<Map<String,Object>> page = new Page<>(1,2);
        page.setCurrent(1);
        page.setSize(2);
        Map<String,Object> param = new HashMap<>();
        param.put("enabled",1);
        param.put("id",1);
        return testMapper.selectPage(page,param);
    }

    @GetMapping("mybatis02")
    @ResponseBody
    public Integer insertUserList(){
        Page<Map<String,Object>> page = new Page<>();
        page.setPages(1);
        page.setSize(1);
        ArrayList<UserPO> users = new ArrayList<>();
        for(int i=0; i<10; i++){
            UserPO user = new UserPO();
            String substring = UUID.randomUUID().toString().substring(0, 4);
            user.setUsername(substring);
            user.setPassword(substring+"_piggy");
            user.setEnabled((short)1);
            users.add(user);
        }
        return testMapper.insertBatch(users);
    }
    @GetMapping("mybatis03")
    @ResponseBody
    public Integer insertOne(){
        UserPO user = new UserPO();
        String substring = UUID.randomUUID().toString().substring(0, 4);
        user.setEnabled((short)1);
        user.setUsername(substring);
        user.setPassword(substring+"_piggy");
        int i = testMapper.insertOne(user);
        System.out.println(user.getId());
        return i;
    }

}
